import java.io.Serializable;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class RWHashMap<K, V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
    private HashMap<K,V> hashMap;
    private final ReentrantReadWriteLock rw = new ReentrantReadWriteLock();
    public final Lock r = rw.readLock();
    public final Lock w = rw.writeLock();
    public RWHashMap(int initialCapacity){
        hashMap = new HashMap<K,V>(initialCapacity);
    }
    @Override
    public V get(Object key){
        r.lock();
        try{
            return hashMap.get(key);
        }finally {
            r.unlock();
        }
    }
    @Override
    public Set<Entry<K,V>> entrySet(){
        r.lock();
        try{
            return hashMap.entrySet();
        }finally {
            r.unlock();
        }
    }
    @Override
    public V put(K key, V value){
        w.lock();
        try{
            return hashMap.put(key,value);
        }finally {
            w.unlock();
        }

    }
}
